import uuid

from django.db import models
from django.db.models import SET_NULL

from apps.ExamManagement.models import Exam
from apps.QuestionManagement.models import Questions


class Grade(models.Model):
    class Meta:
        db_table = 'grade'

    objects = models.Manager()
    id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255)
    # 绑定考试ID
    exam = models.ForeignKey(Exam, null=True, max_length=255, on_delete=models.CASCADE, help_text='绑定考试ID')

    # 统计平均分
    average_score = models.IntegerField(null=True, blank=True, verbose_name='平均分', default=0)

    # 统计通过率  100.00%
    pass_rate = models.DecimalField(null=True, blank=True, verbose_name='通过率', default=0, max_digits=5, decimal_places=2)

    # 应考人数
    should_num = models.IntegerField(null=True, blank=True, verbose_name='应考人数', default=0)

    # 实考人数
    actual_num = models.IntegerField(null=True, blank=True, verbose_name='实考人数', default=0)


class GradeDetail(models.Model):
    class Meta:
        db_table = 'grade_detail'

    objects = models.Manager()
    id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255)

    question = models.ForeignKey(Questions, on_delete=models.CASCADE)

    # 可以为空，为空时统计在所有考试中的正确率
    exam = models.ForeignKey(Exam, null=True, on_delete=models.CASCADE)

    # 统计正确率
    correct_rate = models.DecimalField(null=True, blank=True, verbose_name='正确率', default=0, max_digits=5, decimal_places=2)